package com.zhuoyue.web.controller.system;

import com.zhuoyue.common.annotation.Log;
import com.zhuoyue.common.constant.UserConstants;
import com.zhuoyue.common.core.controller.BaseController;
import com.zhuoyue.common.core.domain.AjaxResult;
import com.zhuoyue.common.core.domain.entity.SysImageCategory;
import com.zhuoyue.common.enums.BusinessType;
import com.zhuoyue.common.utils.SecurityUtils;
import com.zhuoyue.common.utils.StringUtils;
import com.zhuoyue.system.domain.TblProductInfo;
import com.zhuoyue.system.mapper.ProductInfoMapper;
import com.zhuoyue.system.service.ISysImageCategoryService;
import io.swagger.annotations.Api;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Iterator;
import java.util.List;

/**
 * 图片分类信息
 *
 * @author hewenze
 */
@RestController
@Api(value = "图片分类管理接口", tags = "图片分类管理接口")
@RequestMapping("/system/imageCategory")
public class SysImageCategoryController extends BaseController {
    @Autowired
    private ISysImageCategoryService sysImageCategoryService;
    @Autowired
    private ProductInfoMapper sysImageMapper;

    /**
     * 获取分类列表
     */
    @GetMapping("/list")
    public AjaxResult list(SysImageCategory sysImageCategory) {
        List<SysImageCategory> list = sysImageCategoryService.selectImageCategoryList(sysImageCategory);
        return AjaxResult.success(list);
    }

    /**
     * 查询分类列表（排除节点）
     */
    @GetMapping("/list/exclude/{categoryId}")
    public AjaxResult excludeChild(@PathVariable(value = "categoryId", required = false) Long categoryId) {
        List<SysImageCategory> sysImageCategoryList = sysImageCategoryService.selectImageCategoryList(new SysImageCategory());
        Iterator<SysImageCategory> it = sysImageCategoryList.iterator();
        while (it.hasNext()) {
            SysImageCategory d = (SysImageCategory) it.next();
            if (d.getCategoryId().intValue() == categoryId
                    || ArrayUtils.contains(StringUtils.split(d.getAncestors(), ","), categoryId + "")) {
                it.remove();
            }
        }
        return AjaxResult.success(sysImageCategoryList);
    }

    /**
     * 根据图片分类编号获取详细信息
     */
    @GetMapping(value = "/{categoryId}")
    public AjaxResult getInfo(@PathVariable Long categoryId) {
        return AjaxResult.success(sysImageCategoryService.selectImageCategoryById(categoryId));
    }

    /**
     * 获取分类下拉树列表
     */
    @GetMapping("/treeselect")
    public AjaxResult treeselect(SysImageCategory sysImageCategory) {
        List<SysImageCategory> sysImageCategoryList = sysImageCategoryService.selectImageCategoryList(sysImageCategory);
        return AjaxResult.success(sysImageCategoryService.buildDeptTreeSelect(sysImageCategoryList));
    }

    /**
     * 新增分类
     */
    @Log(title = "分类管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody SysImageCategory sysImageCategory) {
        if (UserConstants.NOT_UNIQUE.equals(sysImageCategoryService.checkDeptNameUnique(sysImageCategory))) {
            return AjaxResult.error("新增分类'" + sysImageCategory.getCategoryName() + "'失败，分类名称已存在");
        }
        sysImageCategory.setCreateBy(SecurityUtils.getUsername());
        return toAjax(sysImageCategoryService.insertDept(sysImageCategory));
    }

    /**
     * 修改分类
     */
    @Log(title = "分类管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody SysImageCategory sysImageCategory) {
        if (UserConstants.NOT_UNIQUE.equals(sysImageCategoryService.checkDeptNameUnique(sysImageCategory))) {
            return AjaxResult.error("修改分类'" + sysImageCategory.getCategoryName() + "'失败，分类名称已存在");
        } else if (sysImageCategory.getParentId().equals(sysImageCategory.getCategoryId())) {
            return AjaxResult.error("修改分类'" + sysImageCategory.getCategoryName() + "'失败，上级分类不能是自己");
        } else if (StringUtils.equals(UserConstants.DEPT_DISABLE, sysImageCategory.getStatus())
                && sysImageCategoryService.selectNormalChildrenDeptById(sysImageCategory.getCategoryId()) > 0) {
            return AjaxResult.error("该部门包含未停用的子分类！");
        }
        sysImageCategory.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(sysImageCategoryService.updateDept(sysImageCategory));
    }

    /**
     * 删除分类
     */
    @Log(title = "分类管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{categoryId}")
    public AjaxResult remove(@PathVariable Long categoryId) {
        if (sysImageCategoryService.hasChildByDeptId(categoryId)) {
            return AjaxResult.error("存在下级分类,不允许删除");
        }
        List<TblProductInfo> sysImages = sysImageMapper.selectProductInfoByCategoryId(categoryId);
        if (sysImages.size() > 0) {
            return AjaxResult.error("此分类下有数据,不允许删除");
        }

        return toAjax(sysImageCategoryService.deleteDeptById(categoryId));
    }
}
